/**
 * Created by George on 2015-06-28.
 */
'use strict';


/* 模块定义
 * 注明每个模块含义
 */

// Koa 框架
var koa = require('koa');
// HTTP 服务模块
var http = require('http');
var bodyParser = require('koa-better-body');

var jsonp = require('koa-safe-jsonp');

var router = require('koa-router')();
var O2OServiceRoute = require('../routes')(router);
var C = require('./../config/index');
var cors = require('koa-cors');//解决跨域

var gzip = require('koa-gzip');//压缩页面

// 实例化 Koa
var app = koa();
app.use(gzip());

// x-response-time
app.use(function *(next) {
    console.log(this.url);
    // (1) 进入路由
    var start = new Date;
    yield next;
    // (5) 再次进入 x-response-time 中间件，记录2次通过此中间件「穿越」的时间
    var ms = new Date - start;
    this.set('X-Response-Time', ms + 'ms');
    // (6) 返回 this.body
});


app.proxy = false;//do not support `X-Forwarded-*` header from 2015-11-23

app.use(cors({credentials: true}));

app.use(bodyParser());

// Session 名
//app.keys = ['keys', 'keykeys'];

app.use(router.routes());

jsonp(app, {
    callback: '_callback', // default is 'callback'
    limit: 50 // max callback name string length, default is 512
});


//var mPubuIM = require('../models/mPubuIM.js');
app.on('error', function (err, ctx) {
    console.error('ERROR ON App.');
    console.error(err);

    ////log.error('server error', err, ctx);
    //
    ////描述
    //let dec = "";
    //dec += "err: " + JSON.stringify(err);
    //dec += "   ctx.I: " + JSON.stringify(ctx.I);
    //dec += "   originalUrl: " + ctx.originalUrl;
    ////配置
    //let temp = {};
    //temp.text = "有错,请留意.";
    //temp.title = err.message || "";
    //temp.description = dec || "";
    ////temp.url = "";
    //temp.color = "error";
    ////实例
    //var pubuIM = new mPubuIM(temp);
    //pubuIM.send();
    //
});

//appServer = http.createServer(app.callback());
//
//if (!module.parent) {
//    appServer.listen(C.o2oService_port);
//}


module.exports = app;